﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotabuffWebAPI;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;

namespace SteamWebAPI
{
    /// <summary>
    /// Identifikátor uživatele steamu
    /// </summary>
    [Serializable()]
    public class SteamUser : IEquatable<SteamUser>
    {

        [System.Xml.Serialization.XmlElement("personaname")]
        public string Name
        {
            get;
            set;
        }

        [System.Xml.Serialization.XmlElement("visibility")]
        public int DotaAccountVisibilityLevel
        {
            get;
            set;
        }

        [System.Xml.Serialization.XmlElement("communityvisibilitystate")]
        public int AccountVisibilityLevel
        {
            get;
            set;
        }

        [System.Xml.Serialization.XmlElement("realname")]
        public string RealName
        {
            get;
            set;
        }

        private int group;

        [System.Xml.Serialization.XmlElement("group")]
        public int Group
        {
            get { return group;}
            set { group = value;}
        }

        [System.Xml.Serialization.XmlElement("id")]
        public string SteamID
        {
            get;
            set;
        }

        [System.Xml.Serialization.XmlElement("id64")]
        public Int64 Steam64
        {
            get;
            set;
        }

        [System.Xml.Serialization.XmlElement("loccountrycode")]
        public string CountryTag
        {
            get;
            set;
        }

        [System.Xml.Serialization.XmlArray("players")]
        [System.Xml.Serialization.XmlArrayItem("player", typeof(DateTime))]
        public ObservableCollection<DateTime> LastTimePlayed
        {
            get;
            set;
        }

        [System.Xml.Serialization.XmlElement("avatarmedium")]
        public string AvatarMedium
        {
            set 
            {
                if (value != String.Empty)
                {
                    Uri uri = new Uri(value);
                    this.Avatar = uri;
                }

            }
            get
            {
                if (Avatar != null)
                {
                    return Avatar.AbsoluteUri;
                }
                else
                {
                    return String.Empty;
                }
                
            }
        }

        /// <summary>
        /// Statistiky hráče
        /// </summary>
        private DotabuffWebAPI.PlayerInfo info;

        /// <summary>
        /// Přátelé hráče.
        /// </summary>
        [System.Xml.Serialization.XmlIgnore]
        public ObservableCollection<SteamUser> Friends = new ObservableCollection<SteamUser>();



        private Uri avatar;
        [System.Xml.Serialization.XmlIgnore]
        public Uri Avatar
        {
            get {return this.avatar ;}
            set {this.avatar = value ;}
        }

        //[System.Xml.Serialization.XmlIgnore]
        public DotabuffWebAPI.PlayerInfo Info
        {
            get { return this.info; }
            set { this.info = value; }
        }

        [System.Xml.Serialization.XmlIgnore]
        public Dota2WebAPI.MatchOverview Overview
        {
            get;
            set;
        }

        public override string ToString()
        {
            return this.Name;
        }

        public SteamUser()
        {
            this.SteamID = "Invalid";
            Name = "";
            info = null;
            CountryTag = "×";
            this.Group = 0;
            AccountVisibilityLevel = 0;
            DotaAccountVisibilityLevel = 0;
            LastTimePlayed = new ObservableCollection<DateTime>();
        }

        public SteamUser(string steamID)
        {
            this.Name = steamID;
            this.SteamID = steamID;
            this.Steam64 = SteamUser.ConvertID(steamID);
            this.Group = 0;
            AccountVisibilityLevel = 0;
            DotaAccountVisibilityLevel = 0;
            LastTimePlayed = new ObservableCollection<DateTime>();
        }

        public SteamUser(Int64 steam64)
        {
            this.Steam64 = steam64;
            this.Group = 0;
            AccountVisibilityLevel = 0;
            DotaAccountVisibilityLevel = 0;
            LastTimePlayed = new ObservableCollection<DateTime>();
        }

        public void FindFriends(string steamID, bool fetchStats = true)
        {
            this.SteamID = steamID;
            this.Steam64 = SteamUser.ConvertID(steamID);
            // Fetchneme si přátele
            try
            {
                SteamQuery query = new SteamQuery();
                this.Friends = query.GetUserFriends(SteamUser.ConvertID(steamID));
            }
            catch (SystemException systemException)
            {
                Console.WriteLine(systemException.ToString());
            }

            // Mají se získat i data o hráči?
            if (fetchStats)
            {
                DotabuffWebAPI.Query query = new Query();
                this.info = query.GetPlayerInfo(steamID);
            }
        }

        public static Int64 ConvertID(string id)
        {
            int iServer = 0;
            int iAuthID = 0;
            string bogus = "";
            string server = "";
            string authID = "";

            char[] szAuthID = new char[64];

            string pattern = @"\d+";

            Match result = Regex.Match(id, pattern);
            if (result.Success)
            {
                bogus = result.Value;
                result = result.NextMatch();
            }
            if (result.Success)
            {
                server = result.Value;
                result = result.NextMatch();
            }
            if (result.Success)
            {
                authID = result.Value;
                result.NextMatch();
            }
            iServer = Convert.ToInt32(server);
            iAuthID = Convert.ToInt32(authID);

            if (iAuthID == 0)
            {
                return 0;
            }


            Int64 resultID = (Int64)iAuthID * 2;
            resultID += 76561197960265728 + iServer;
            return resultID;
        }



        public bool Equals(SteamUser other)
        {
            return (this.Steam64 == other.Steam64);
        }
    }


}
